package com.jxs.android.ges.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jxs.android.ges.GesApplication;

public class CustomerHolder {

	static public interface Table {
		public static final String TABLE_NAME = "ges_customer_tb";

		public static final String ID = "i_id";
		public static final String NICK_NAME = "i_nickName";

		public static final String COLUMNS[] = new String[] { ID, NICK_NAME };
	}

	public static void creater(SQLiteDatabase db) {
		StringBuffer createSQL = new StringBuffer(" CREATE TABLE IF NOT EXISTS ");
		createSQL.append(Table.TABLE_NAME);
		createSQL.append(" (_id INTEGER PRIMARY KEY AUTOINCREMENT, ");
		createSQL.append(Table.ID + " varchar(20),");
		createSQL.append(Table.NICK_NAME + " varchar(100)");
		createSQL.append(" )");
		db.execSQL(createSQL.toString());
	}

	public static void saveOrUpdate(String phone, String nickName) {
		ContentResolver contentResolver = GesApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(CustomerProvider.CONTENT_URI, Table.COLUMNS, Table.ID + " =?  ", new String[] { phone }, null);
		ContentValues values = new ContentValues();
		values.put(Table.ID, phone);
		values.put(Table.NICK_NAME, nickName);
		if (null != cursor && cursor.getCount() <= 0) {
			contentResolver.insert(CustomerProvider.CONTENT_URI, values);
		} else {
			contentResolver.update(CustomerProvider.CONTENT_URI, values, Table.ID + " =?  ", new String[] { phone });
		}
		if (null != cursor)
			cursor.close();
	}

	public static boolean isNeedUpdate() {
		boolean isNeed = false;
		ContentResolver contentResolver = GesApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(CustomerProvider.CONTENT_URI, Table.COLUMNS, null, null, null);
		if (null != cursor) {
			isNeed = cursor.getCount() <= 0;
		}
		if (null != cursor) {
			cursor.close();
		}
		return isNeed;
	}

	public static List<Map<String, String>> queryAll(boolean notHasAll) {
		List<Map<String, String>> res = new ArrayList<Map<String, String>>();
		ContentResolver contentResolver = GesApplication.getApplication().getContentResolver();
		Cursor cursor = null;

		cursor = contentResolver.query(CustomerProvider.CONTENT_URI, Table.COLUMNS, null, null, null);
		if (null != cursor && cursor.moveToFirst()) {
			if (!notHasAll) {
				Map<String, String> rests = new HashMap<String, String>();
				rests.put("id", "");
				rests.put("content", "全部");
				res.add(rests);
			}
			do {
				Map<String, String> results = new HashMap<String, String>();
				results.put("id", cursor.getString(cursor.getColumnIndex(Table.ID)) + "");
				results.put("content", cursor.getString(cursor.getColumnIndex(Table.NICK_NAME)) + "");
				res.add(results);
			} while (cursor.moveToNext());
		}
		if (null != cursor) {
			cursor.close();
		}
		return res;
	}

	public static String query(String cst_id) {
		String nickName = "";
		ContentResolver contentResolver = GesApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(CustomerProvider.CONTENT_URI, Table.COLUMNS, Table.ID + " =?  ", new String[] { cst_id }, null);
		if (null != cursor && cursor.moveToFirst()) {
			nickName = cursor.getString(cursor.getColumnIndex(Table.NICK_NAME)) + "";
		}
		if (null != cursor) {
			cursor.close();
		}
		return nickName;
	}

}
